package org.linlinjava.litemall.wx.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.wx.annotation.LogAnno;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.dto.WxCollectDTO;
import org.linlinjava.litemall.wx.service.WxCollectService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
/**
 * 用户收藏服务
 *
 * @author yliyun
 * @date 2022/09/14
 */
@Api(tags = "用户收藏服务API")
@RestController
@RequestMapping("/wx/collect")
@Validated
@RequiredArgsConstructor
public class WxCollectController {

    /**
     * 用户收藏服务
     */
    private final WxCollectService wxCollectService;

    /**
     * 用户收藏添加或删除
     * <p>
     * 如果商品没有收藏，则添加收藏；如果商品已经收藏，则删除收藏状态。
     *
     * @param userId       用户ID
     * @param wxCollectDto wx收集dto
     * @return 操作结果
     */
    @ApiOperation(value = "用户收藏添加或删除")
    @PostMapping("addorupdate")
    @LogAnno
    public Object addorupdate(@ApiIgnore @LoginUser Integer userId, @RequestBody WxCollectDTO wxCollectDto) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return wxCollectService.addorupdate(userId, wxCollectDto);
    }

    /**
     * 删除
     *
     * @param userId  用户id
     * @param goodsId 商品id
     * @return {@link Object}
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("delete")
    @LogAnno
    public Object delete(@LoginUser Integer userId,@NotNull Integer goodsId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return wxCollectService.delete(userId, goodsId);
    }

    /**
     * 是收藏
     *
     * @param userId  用户id
     * @param goodsId 商品id
     * @return {@link Object}
     */
    @ApiOperation(value = "是收藏")
    @ApiImplicitParam(name = "goodsId",value = "商品id")
    @GetMapping("isCollect")
    @LogAnno
    public Object isCollect(@ApiIgnore@LoginUser Integer userId,@RequestParam Integer goodsId){
       return  wxCollectService.isCollect(userId,goodsId);
    }

    /**
     * 用户收藏列表
     *
     * @param userId 用户id
     * @return {@link ResponseUtil.Response}
     */
    @ApiOperation(value = "收藏列表")
    @GetMapping(value = "userCollect")
    @LogAnno
    public ResponseUtil.Response userCollect(@ApiIgnore @LoginUser Integer userId){
        return wxCollectService.userCollect(userId);
    }

}
